package com.creditcmsplt.util;

import java.awt.Font;
import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PDFTextReader {
	
	private static Logger log = LoggerFactory.getLogger(PDFTextReader.class);

	private static String getTextFromPDF(String pathname) throws Exception {
		//String pathname = "D:/12365.pdf";
		RandomAccessRead accessRead = new org.apache.pdfbox.io.RandomAccessFile(new File(pathname), "rw");
		PDFParser parser = new PDFParser(accessRead); // 创建PDF解析器
		parser.parse(); // 执行PDF解析过程
		PDDocument pdfdocument = parser.getPDDocument(); // 获取解析器的PDF文档对象
		PDFTextStripper pdfstripper = new PDFTextStripper(); // 生成PDF文档内容剥离器
		String contenttxt = pdfstripper.getText(pdfdocument); // 利用剥离器获取文档
		accessRead.close();
		pdfdocument.close();
		return contenttxt;
	}

	private static void saveExcel(String name,String idno,String filePath) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFFont font1 = wb.createFont();
		HSSFFont font2 = wb.createFont();
		font1.setFontName("黑体");
		font1.setFontHeightInPoints((short)24);
		font2.setFontName("黑体");
		font2.setFontHeightInPoints((short)12);
		HSSFCellStyle cellStyle1=wb.createCellStyle();
		cellStyle1.setAlignment(HorizontalAlignment.CENTER);
		cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
		cellStyle1.setWrapText(true);
		cellStyle1.setFont(font1);
		HSSFCellStyle cellStyle2=wb.createCellStyle();
		cellStyle2.setAlignment(HorizontalAlignment.CENTER);
		cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
		cellStyle2.setWrapText(true);
		cellStyle2.setFont(font2);
		HSSFSheet sheet = wb.createSheet("征信报告");
		sheet.autoSizeColumn(3,true);
		HSSFRow row = sheet.createRow(0);
		//row.setHeight(Short.valueOf("700"));
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("信用报告查询结果");
		cell.setCellStyle(cellStyle1);
		row.setHeightInPoints(20); 
		row.setHeight((short)(25*20));
		HSSFRow row2 = sheet.createRow(1);
		row2.setHeightInPoints(20); 
		row2.setHeight((short)(25*20));
		HSSFCell cell1 = row2.createCell(0);
		cell1.setCellValue("被查询人信息");
		cell1.setCellStyle(cellStyle1);
		HSSFRow row3 = sheet.createRow(2);
		//row3.setHeight(Short.valueOf("400"));
		HSSFCell cname = row3.createCell(0);
		cname.setCellValue("姓名");
		cname.setCellStyle(cellStyle2);
		HSSFCell cnamevalue = row3.createCell(1);
		cnamevalue.setCellValue(name);
		cnamevalue.setCellStyle(cellStyle2);
		HSSFCell cidno = row3.createCell(2);
		cidno.setCellValue("证件类型");
		cidno.setCellStyle(cellStyle2);
		HSSFCell cidnovalue = row3.createCell(3);
		cidnovalue.setCellValue("身份证");
		cidnovalue.setCellStyle(cellStyle2);
		HSSFRow row4 = sheet.createRow(3);
		//row4.setHeight(Short.valueOf("400"));
		HSSFCell cid = row4.createCell(0);
		cid.setCellValue("证件号码");
		cid.setCellStyle(cellStyle2);
		HSSFCell cidv = row4.createCell(1);
		cidv.setCellValue(idno);
		cidv.setCellStyle(cellStyle2);
		HSSFRow row5 = sheet.createRow(4);
		//row5.setHeight(Short.valueOf("400"));
		HSSFCell cr5 = row5.createCell(0);
		cr5.setCellValue("查询结果");
		cr5.setCellStyle(cellStyle2);
		
		HSSFRow row6 = sheet.createRow(5);
		//row6.setHeight(Short.valueOf("400"));
		HSSFCell cr6 = row6.createCell(0);
		cr6.setCellValue("个人征信系统中没有此人的征信记录");
		cr6.setCellStyle(cellStyle2);
		
		sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
		sheet.addMergedRegion(new CellRangeAddress(1,1,0,3));
		sheet.addMergedRegion(new CellRangeAddress(3,3,1,3));
		sheet.addMergedRegion(new CellRangeAddress(4,4,0,3));
		sheet.addMergedRegion(new CellRangeAddress(5,5,0,3));
		
		sheet.setColumnWidth(0, 256*20);
		sheet.setColumnWidth(1, 256*20);
		sheet.setColumnWidth(2, 256*20);
		sheet.setColumnWidth(3, 256*20);
		try {
			wb.write(new File(filePath));
			wb.close();
		} catch (IOException e) {
			log.error("",e);
		}
	}
	
	public static void noStatusConvert(String pdfPath,String filePath) {
		try {
			String abc=getTextFromPDF(pdfPath);
			abc=abc.replaceAll("\n", "");
			abc=abc.replaceAll("\r", "");
			boolean iscontue=abc.contains("查询结果")&&abc.contains("个人征信系统中没有此人的征信记录")&&abc.length()<=320;
			if(iscontue) {
				String tep[]=abc.split(" ");
				String name="";
				String idn="";
				Integer nameIndex=null;
				Integer idnIndex=null;
				for (int i = 0; i < tep.length; i++) {
					if("姓名".equals(tep[i])) {
						nameIndex=i+1;
					}else if("证件号码".equals(tep[i])) {
						idnIndex=i+1;
					}
				}
				name=tep[nameIndex];
				//name=name.
				idn=tep[idnIndex];
				saveExcel(StringReplaceUtil.nameHide(name), StringReplaceUtil.idHide(idn),filePath);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
	
	

	public static void main(String[] args) {
		noStatusConvert("D:/12365.pdf", "D:/12365.xls");
	}
}
